package cn.shengchao.examstar.ai.application.service;

import cn.shengchao.examstar.ai.infrastructure.external.ai.service.DifyChatflowClientService;
import cn.shengchao.examstar.ai.infrastructure.external.ai.service.res.KnowledgePointDescriptionDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * AI知识点服务
 * 处理知识点相关的AI功能，如生成知识点描述
 *
 * @author examstar
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AiKnowledgeService {

    private final DifyChatflowClientService difyChatflowClientService;

    /**
     * 生成知识点描述
     * 为指定的知识点生成核心概念、重点内容和学习建议
     *
     * @param userId 用户ID
     * @param knowledgePointName 知识点名称
     * @return 知识点描述DTO，包含核心概念、重点内容和学习建议
     * @throws IOException 如果API调用出错
     */
    public KnowledgePointDescriptionDTO generateKnowledgeDescription(
            Long userId,
            String knowledgePointName, String subject) throws IOException {

        log.info("开始为知识点生成描述 - 用户ID: {}, 知识点名称: {}",
                userId, knowledgePointName);
        // 构建请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("knowledge_point_name", knowledgePointName);
        params.put("subject", subject);

        // 调用Dify工作流服务
        KnowledgePointDescriptionDTO result = difyChatflowClientService
                .executeGenerateKnowledgeDescriptionWorkflow(userId, params);

        log.info("知识点描述生成完成 - 知识点: {}", knowledgePointName);
        return result;
    }
} 